<?php
  $tournaments;
  $idTournament;
  if (!isset($_GET['idTournament'])) {
    $tournaments = mysql_fetch_array(mysql_query(
        'SELECT * FROM tournament ORDER BY year DESC LIMIT 0,1'));
    $idTournament = $tournaments['idTournament'];
    $year = $tournaments['year'];
  } else  {
  	$tournaments = mysql_fetch_array(mysql_query(
        'SELECT * FROM tournament WHERE idTournament = ' . $_GET['idTournament']));
  	$idTournament = $_GET['idTournament'];

  }
  $year = $tournaments['year'];
  $beginDate = $tournaments['beginDate'];
  $firstDate = date('d', strtotime($beginDate));
  $nbDays = 6;
  function getDateD($day) {
  	global $year;
    return $year . '-08-' . (intval($day) < 10 ? '0' : '') . intval($day); 
  }
  function displayTime($time) {
    return ($time < 10 ? '0' : '') . $time;
  }
?>
<style>
  table.matriceField td, table.matriceField th {
    border: grey 1px solid;
    padding: 10px;
  }
  #corp {
    max-width: 2000px;
  }
  #menu {
    display: none;
  }
</style>
<table class="matriceField">
  <thead>
    <tr>
      <th>
      </th>
      <?php for ($i = $firstDate; $i < $firstDate + $nbDays; $i++) { ?>
      <th>
        <?php echo getDateD($i) ?>
      </th>
      <?php } ?>
      <!-- <th>
        Nombre de Matchs
      </th> -->
    </tr>
  </thead>
  <tbody>
    <?php
      $fields = mysql_query(
        'SELECT * FROM tournamentField');
      while ($field = mysql_fetch_array($fields)) {
        $nbMatchs = 0;
    ?>
      <tr>
        <td>
          <?php echo $field['fieldName'] ?>
        </td>
        <?php for ($i = $firstDate; $i < $firstDate + $nbDays; $i++) { ?>
        <td>
          <ul>
            <?php 
              $matchs = mysql_query(
                'SELECT * FROM tournamentMatch 
                  NATURAL JOIN tournamentDraw 
                  NATURAL JOIN tournamentDrawType 
                  NATURAL JOIN tournamentRoundType
                    WHERE date = "' . getDateD($i) . '"
                    AND idTournamentField = ' . $field['idTournamentField'] . '
                    AND idTournament = ' . $idTournament . 
                    ' ORDER BY hour');
              while ($match = mysql_fetch_array($matchs)) {
            ?>
              <li>
                <?php echo $match['hour'] . 'h' . displayTime($match['minute']) . ' '. $match['tournamentDrawTypeName'] . ' : ' .
                    displayMatchSummary($match);
                  $nbMatchs++;
                ?>
              </li>
            <?php } ?>
          </ul>
        </td>
        <?php } ?>
        <!--<td>
          <?php echo $nbMatchs; ?>
        </td> -->
      </tr>
    <?php } ?>
  </tbody>
</table>